package com.yonyou.base.user.web;

import com.yonyou.base.common.utils.EncodePasswordService;
import com.yonyou.base.common.utils.UUIDUtils;
import com.yonyou.base.common.web.MessageResult;
import com.yonyou.base.user.model.UserVO;
import com.yonyou.base.user.service.IUserService;
import com.yonyou.hrcloud.adder.model.query.Condition;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;

/**
 * 用户模块，注册、登录、重置密码
 */
@Controller
@RequestMapping("/user")
public class UserController {

    @Autowired
    private IUserService userService;

    @Autowired
    private EncodePasswordService passwordService;


    /**
     * 用户登陆
     *
     * @param userVO 用户信息
     * @return 用户id
     */
    @RequestMapping("/login.do")
    @ResponseBody
    public Object login(@RequestBody UserVO userVO) {
        MessageResult result = new MessageResult(MessageResult.STATUS_SUCCESS, "操作成功");
        if (userVO.getUsername() == null || userVO.getPassword() == null) {
            return new MessageResult(MessageResult.STATUS_ERROR, "用户名密码不能为空");
        }
        Condition cond = new Condition();
        cond.addExpression("username", userVO.getUsername());
        List<UserVO> users = userService.queryUserListByCondition(cond, null);
        if (users == null || users.size() < 1) {
            return new MessageResult(MessageResult.STATUS_ERROR, "用户名或密码不正确");
        }
        String encodePassword = passwordService.encodePassword(users.get(0).getUsername(), userVO.getPassword());
        if (users.get(0) == null || !users.get(0).getPassword().equals(encodePassword)) {
            return new MessageResult(MessageResult.STATUS_ERROR, "用户名或密码不正确");
        }
        HashMap<String, Object> params = new HashMap<>();
        params.put("userId", users.get(0).getId());
        result.setData(params);
        return result;
    }

    /**
     * 注册用户
     *
     * @param userVO 用户注册信息
     * @return Object 用户id
     */
    @RequestMapping("/register.do")
    @ResponseBody
    public Object register(@RequestBody UserVO userVO) {
        MessageResult result = new MessageResult(MessageResult.STATUS_SUCCESS, "操作成功");
        if (StringUtils.isEmpty(userVO.getUsername()) || StringUtils.isEmpty(userVO.getPassword())) {
            result = MessageResult.ERROR;
            result.setMessage("用户名或密码不能为空");
            return result;
        }

        Condition cond = new Condition();
        cond.addExpression("username", userVO.getUsername());
        List<UserVO> users = userService.queryUserListByCondition(cond, null);
        if (users != null && users.size() > 0) {
            result = MessageResult.ERROR;
            result.setMessage("当前用户名已经注册");
            return result;
        }

        UserVO data = new UserVO();
        data.setUsername(userVO.getUsername());
        data.setCreationtime(System.currentTimeMillis());
        data.setId(UUIDUtils.getRandomUUID());
        data.setPassword(passwordService.encodePassword(userVO.getUsername(), userVO.getPassword()));
        userService.insertUser(data);
        HashMap<String, Object> params = new HashMap<>();
        params.put("userId", data.getId());
        result.setData(params);
        return result;
    }

    /***
     * 重置密码
     *
     * @param user 用户信息
     * @return Object
     */
    @RequestMapping("/resetPassword.do")
    @ResponseBody
    public Object resetPassword(@RequestBody UserVO user) {
        userService.resetUserPassword(user.getId(), user.getUsername(), user.getPassword());
        return MessageResult.SUCCESS;
    }
}
